package com.example.cyclops;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.os.Environment;
import android.util.Log;


public class MythDatabase{
	private String basepath;
	private ArrayList<MythIPtable> iptable;
	private int m_index;
	public MythDatabase(String filepath){
		File extDir = Environment.getExternalStorageDirectory();
		
		basepath = extDir.getPath() + "/Android/data/" + filepath;

		File file = new File(basepath);
		if(!file.exists())
			if(!file.mkdirs())
				Log.e("mythCopy","make dir error");
		basepath += "iptable.mythdb";
		readDatabase();
	}
	public int getListIndex(){
		return m_index;
	}
	public MythIPtable getIndex(){
		if(m_index > iptable.size())
			return null;
		else
			return iptable.get(m_index);
	}
	public MythIPtable get(int index){
		return iptable.get(index);
	}
	public void refresh(){
		iptable = new ArrayList<MythIPtable>();
		readDatabase();
	}
	public void flush()
	{
		FileWriter fileWriter;
		try {
			fileWriter = new FileWriter(basepath);
			fileWriter.write(iptable.size() + "\r\n");
			fileWriter.flush();
			for(int i = 0;i < iptable.size();i++){
				MythIPtable tmpmyth = iptable.get(i);
				fileWriter.write(tmpmyth.getKeyname() + ";"
					+ tmpmyth.getIP() + ";"
					+ tmpmyth.getPort() + ";"
					+ tmpmyth.getUsername() + ";"
					+ tmpmyth.getPassword() + "\r\n");
				fileWriter.flush();
			}
			fileWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delMythIPtable(int index){
		iptable.remove(index);
	}
	//save to iptable
	public void writeMythIPtable(int index,MythIPtable mythiptable){
		if(index == -1 || index > iptable.size()){
			iptable.add(mythiptable);
		}else{
			iptable.set(index, mythiptable);
		}
	}
	public void SetIndex(int index){
		this.m_index = index;
	}
	
	public int size(){
		return iptable.size();
	}
	public void readDatabase(){
		int lockindex = 0;
		iptable = new ArrayList<MythIPtable>();
		try {
            String encoding="utf-8";
            File file=new File(basepath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                	if(lockindex == 0){
                		//m_index = Integer.parseInt(lineTxt);
                	}else{
                		String[] tmpstr = lineTxt.split(";");
                		MythIPtable tmpiptable = new MythIPtable(tmpstr);
                		iptable.add(tmpiptable);
                	}
                	lockindex++;
                }
                read.close();
            }else{
            	file.createNewFile();
            	System.out.println("找不到指定的文件");
            	//this.myvpn = new MythVPN("10.62.162.20","video","123456");
            	MythIPtable mythip = new MythIPtable("测试服务器","1029.mythkast.net","3000"
            			,"root","pass");
            	this.writeMythIPtable(-1, mythip);
            	mythip = new MythIPtable("实验室测试路由","192.168.31.181","3000"
            			,"root","pass");
            	this.writeMythIPtable(-1, mythip);
            	m_index = 0;
            	this.flush();
            }
		}catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
	}
}
